package com.dcliu.db;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;


public class PageRequest implements Serializable {

  private static final long serialVersionUID = -6432314362846469109L;
  
  //每页记录
  private Integer size = 15;
  private Integer page = 1;
  //跳至页数
  private Integer skip = 0;
  // 排序字段名
  private String sortField;
  // 排序默认升序asc
  private String sortOrder;

  public Integer getSize() {
    return size;
  }

  public void setSize(Integer size) {
    this.size = size;
  }

  public Integer getPage() {
    return page;
  }

  public void setPage(Integer page) {
    if (page != null && page > 0) {
      this.page = page;
    }
  }

  public Integer getSkip() {
    if (page > 1) {
      skip = (page - 1) * size;
    }
    return skip;
  }

  public void setSkip(Integer skip) {
    this.skip = skip;
  }

  public String getSortField() {
    return sortField;
  }

  public void setSortField(String sortField) {
    this.sortField = sortField;
  }

  public String getSortOrder() {
    return SortOrder.desc.equals(sortOrder) ? sortOrder : SortOrder.asc.toString();
  }

  public void setSortOrder(String sortOrder) {
    this.sortOrder = sortOrder;
  }

  public enum SortOrder {
    asc, desc;
    public boolean equals(String sortOrder) {
      if (StringUtils.isBlank(sortOrder))
        return false;
      return this.toString().equals(sortOrder);
    }
  }
}
